package com.coder4j.system.controller;

import com.coder4j.starter.web.DataScope;
import com.coder4j.system.entity.SysConfigEntity;
import com.coder4j.system.service.SysConfigService;
import com.coder4j.system.vo.SysConfigQuery;
import com.coder4j.system.vo.SysConfigVO;
import com.coder4j.system.vo.SysConfigEO;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 配置
 * @author keller
 * @date 2024-05-07 18:19:35
 */
@Slf4j
@RestController
@RequestMapping("/sysConfig")
public class SysConfigController {
    @Resource
    private SysConfigService service;

    @Resource
    private HttpServletResponse response;

    /**
     * 按ID查询配置
     * @param id SysConfig.id
     * @return 配置对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询配置",createName = "keller",createTime = "2024-05-07 18:19:35")
    public R<SysConfigVO> getById( Long id){

        log.info("====== SysConfigControllerGetById ====== {}",id);
        return R.success(service.baseGetVOById(id));
    }

    /**
     * 保存配置
     * 有 id 字段，更新；否则新增
     * @param sysConfig SysConfig对象
     * @return SysConfig对象
     */
    @PostMapping("/save")
    @ApiNote(name="保存配置",createName = "keller",createTime = "2024-05-07 18:19:35")
    public R save(@RequestBody SysConfigVO sysConfig){
        log.info("====== SysConfigControllerSave ====== {}",sysConfig);

        if(ObjectUtils.hasEmpty(sysConfig.getGroupId(),sysConfig.getName(),sysConfig.getValue())){
            return R.paramError();
        }

        service.baseSaveVO(sysConfig);
        return R.success();
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @ApiNote(name="查询配置列表",createName = "keller",createTime = "2024-05-07 18:19:35")
    public R<BasePage<SysConfigVO>> getList(@RequestBody SysConfigQuery query){
        if(query == null){
            query = new SysConfigQuery();
        }
        log.info("====== SysConfigControllerGetList ====== {}",query);
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出配置列表",createName = "keller",createTime = "2024-05-07 18:19:35")
    public void export(@RequestBody SysConfigQuery query){
        if(query == null){
            query = new SysConfigQuery();
        }
        log.info("====== SysConfigControllerGetList ====== {}",query);
        List<SysConfigEntity> list = service.baseQueryList(query);
                List<SysConfigEO> eoList = new ArrayList<>();
                for (SysConfigEntity entity : list) {
                    SysConfigEO eo = new SysConfigEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"SysConfigList",eoList);
    }


    /**
     * 删除配置
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id SysConfig.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除配置",createName = "keller",createTime = "2024-05-07 18:19:35")
    public R deleteById( Long id){
        log.info("====== SysConfigControllerDeleteById ======  {}",id);
        service.baseDeleteById(id);
        return R.success();
    }
}
